package com.vms.control.weight;

import java.util.Date;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.vms.LaunchActivityArgs;
import com.vms.Settings.ValueType;
import com.vms.control.weight.input.IValueChanged;
import com.vms.control.weight.model.DateModel.IOnDateChanged;
import com.vms.control.weight.model.WeightModel;
import com.vms.control.weight.ui.DayChangeUi;

public class InputSepActivity extends ActionBarActivity implements IValueChanged , View.OnClickListener, IOnDateChanged {

	@SuppressWarnings("unused")
	private DayChangeUi mDayPanel;
	private WeightModel mWeightModel;
	private ValueType mValueType;
	private ImageButton mIncreaseWeight;
	private ImageButton mDecreaseWeight;
	private TextView mValueDigits;
	private TextView mRecordExistText;
	private TextView mTypeTitle;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.input_activity_sep);
		setupActionBar();
		findView();
		setupLaunchArgs(getIntent());
		mWeightModel = new WeightModel(getApplicationContext(),mValueType);
		mWeightModel.registerOnValueChangeListener(this);
		mWeightModel.registerDateChangeListener(this);
		mDayPanel = new DayChangeUi(this, mWeightModel);
		onValueChange();
	}

	private void findView() {
		mIncreaseWeight = (ImageButton)findViewById(R.id.btn_value_up);
		mIncreaseWeight.setOnClickListener(this);
		mDecreaseWeight = (ImageButton)findViewById(R.id.btn_value_down);
		mDecreaseWeight.setOnClickListener(this);
		mValueDigits = (TextView)findViewById(R.id.value);
		mRecordExistText = (TextView)findViewById(R.id.record_exist_text);
		mTypeTitle = (TextView)findViewById(R.id.value_type_title);
	}

	private void setupLaunchArgs(Intent intent) {
		LaunchActivityArgs args = (LaunchActivityArgs) intent.getExtras().getSerializable(InputActivity.LAUNCH_ACTIVITY_ARGS);
		mValueType = args.getType();
		updateTitle();
	}

	private void updateTitle() {
		mTypeTitle.setText(mValueType.equals(ValueType.WEIGHT_MORNING)? 
				R.string.morning : R.string.evening);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.input_activity_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
			
		case R.id.save:
			mWeightModel.saveControlValues();
			this.finish();
			return true;
			
		case R.id.cancel:
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onValueChange() {
		mValueDigits.setText(mWeightModel.getCurrentValueFor(mValueType));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_value_up:
			mWeightModel.increaseValue(mValueType);
			break;
		case R.id.btn_value_down:
			mWeightModel.decreaseValue(mValueType);
			break;
		default:
			break;
		}
	}

	@Override
	public void onDateChanged(Date date, boolean isToday) {
		showAlreadyExistMessage(!mWeightModel.getValuesForSelectedDate().isEmpty());
	}
	
	public void showAlreadyExistMessage( boolean recordExist ) {
		mRecordExistText.setVisibility(recordExist?View.VISIBLE:View.INVISIBLE);
	}

}
